package edu.scau.divineplace.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import edu.scau.divineplace.entity.enums.IsDeletedEnum;
import edu.scau.divineplace.entity.enums.IsReadEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author: x1aolone
 * @Date: 2020/12/30 17:16
 * MybatisPlus的自动填充功能
 */
@Slf4j
@Component
public class AutoFieldFill implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "isRead", IsReadEnum.class, IsReadEnum.IS_READ_FALSE);
        this.strictInsertFill(metaObject, "isDeleted", Integer.class, IsDeletedEnum.IS_DELETED_FALSE.getType());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}